﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CommonTools
{
    public partial class SplashScreen : Form
    {
        public SplashScreen()
        {
            InitializeComponent();
        }

        private static SplashScreen _current = new SplashScreen();

        public static void ShowSplash(string text = null)
        {
            if (text != null) Message = text;

            _current.Owner = OwnerForm;
            _current.Show();
            _current.Refresh();

            EnableOwner(false);
        }

        public static Form OwnerForm { get; set; }

        public static bool DisableParent { get; set; }

        public static void HideSplash()
        {
            EnableOwner(true);

            _current.Owner = null;
            _current.Hide();
        }

        public static void EnableOwner(bool enabled)
        {
            if (OwnerForm != null && DisableParent) OwnerForm.Enabled = enabled;
        }

        delegate void InvokeDelegate(string message);
        public static void SetMessage(string message)
        {
            if (_current.InvokeRequired)
                _current.Invoke(new InvokeDelegate(SetMessage), new object[] { message });
            else
            {
                _current.lblMessage.Text = message;
                _current.Refresh();
            }
        }

        public static string Message
        {
            get { return _current.lblMessage.Text; }
            set { SetMessage(value); }
        }

        private void SplashScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
